#!/usr/bin/env python3
"""
用户数据模型
"""

from datetime import datetime

from sqlalchemy.orm import mapped_column, Mapped
from sqlalchemy.sql.sqltypes import Boolean, DateTime, Integer, String, Text

from app.models import Base


class User(Base):
    """用户信息表"""

    __tablename__ = "users"

    # 主键
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)

    # 用户基本信息
    username: Mapped[str] = mapped_column(String(50), unique=True, index=True, comment="用户名")
    password_hash: Mapped[str] = mapped_column(String(255), comment="密码哈希")
    name: Mapped[str] = mapped_column(String(100), comment="姓名")
    email: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="邮箱")
    phone: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="手机号")

    # 角色和权限
    role: Mapped[str] = mapped_column(
        String(20),
        default="guard",
        comment="用户角色: guard-守卫, admin-管理员, super-admin-超级管理员"
    )
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否激活")
    permissions: Mapped[str | None] = mapped_column(Text, nullable=True, comment="权限列表，JSON格式")

    # 系统信息
    last_login: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="最后登录时间")
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, comment="创建时间")
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")

    def __repr__(self) -> str:
        return f"<User(id={self.id}, username='{self.username}', role='{self.role}')>"